package com.zx.cashiersystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.cashiersystem.cashiersystem.R;
import com.zx.cashiersystem.utils.ContantsIntent;
import com.zx.cashiersystem.utils.DiscountInputFilter;
import com.zx.cashiersystem.utils.DiscountPay;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/13.
 * 优惠折扣
 */

public class DisCountActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvDiscount;
    private TextView tvFavorable;
    private LinearLayout llFavorable;
    private ImageView ivFavorableNumber;
    private LinearLayout llDiscount;
    private TextView tvDiscount1;
    private TextView tvDiscount2;
    private TextView tvDiscount3;
    private TextView tvDiscount4;
    private TextView tvDiscount5;
    private TextView tvDiscount6;
    private TextView tvDiscount7;
    private TextView tvDiscount8;
    private TextView tvDiscount9;
    private TextView tvDiscount10;
    private EditText etFavorableNumber;
    private EditText etFavorableParticulars;
    private EditText etDiscountPay;
    private CheckBox ckDiscount;
    public List<View> discountList = new ArrayList<>();
    public List<View> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discount);
    }

    @Override
    protected void initView() {
        tvDiscount = (TextView) findViewById(R.id.tv_discount);
        tvFavorable = (TextView) findViewById(R.id.tv_favorable);
        llFavorable = (LinearLayout) findViewById(R.id.ll_favorable);
        ivFavorableNumber = (ImageView) findViewById(R.id.iv_favorable_number);
        llDiscount = (LinearLayout) findViewById(R.id.ll_discount);
        tvDiscount1 = (TextView) findViewById(R.id.tv_discount1);
        tvDiscount2 = (TextView) findViewById(R.id.tv_discount2);
        tvDiscount3 = (TextView) findViewById(R.id.tv_discount3);
        tvDiscount4 = (TextView) findViewById(R.id.tv_discount4);
        tvDiscount5 = (TextView) findViewById(R.id.tv_discount5);
        tvDiscount6 = (TextView) findViewById(R.id.tv_discount6);
        tvDiscount7 = (TextView) findViewById(R.id.tv_discount7);
        tvDiscount8 = (TextView) findViewById(R.id.tv_discount8);
        tvDiscount9 = (TextView) findViewById(R.id.tv_discount9);
        tvDiscount10 = (TextView) findViewById(R.id.tv_discount10);
        etFavorableNumber = (EditText) findViewById(R.id.et_favorable_number);
        etFavorableParticulars = (EditText) findViewById(R.id.et_favorable_particulars);
        etDiscountPay = (EditText) findViewById(R.id.et_discount_pay);
        ckDiscount = (CheckBox) findViewById(R.id.ck_discount);
        inputEdit();


    }

    //初始化输入框过滤器
    public void inputEdit() {
        DiscountInputFilter[] filter2 = {new DiscountInputFilter()};
        etDiscountPay.setFilters(filter2);
    }

    @Override
    protected void initEvent() {
        tvDiscount.setOnClickListener(this);
        tvFavorable.setOnClickListener(this);
        tvDiscount1.setOnClickListener(this);
        tvDiscount2.setOnClickListener(this);
        tvDiscount3.setOnClickListener(this);
        tvDiscount4.setOnClickListener(this);
        tvDiscount5.setOnClickListener(this);
        tvDiscount6.setOnClickListener(this);
        tvDiscount7.setOnClickListener(this);
        tvDiscount8.setOnClickListener(this);
        tvDiscount9.setOnClickListener(this);
        tvDiscount10.setOnClickListener(this);
        ivFavorableNumber.setOnClickListener(this);
        llDiscount.setOnClickListener(this);
        final Intent intent = new Intent(this,PayActivity.class);
        ckDiscount.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (ckDiscount.isChecked()) {  //如果是选着的
                    setClick(true);  //关闭所有点击
                    etDiscountPay.setEnabled(false);
                    if (llDiscount.getVisibility() == View.VISIBLE) {
                        //将折扣的方式以及几折带回，重新计算金额
                        String discountStr = etDiscountPay.getText().toString().trim();
                        intent.putExtra(ContantsIntent.DISCOUNTNUM,discountStr);
                        intent.putExtra(ContantsIntent.FAVORABLENUMBER,"");
                    } else {
                        String favorableNumberStr = etFavorableNumber.getText().toString().trim();
                        intent.putExtra(ContantsIntent.DISCOUNTNUM,"");
                        intent.putExtra(ContantsIntent.FAVORABLENUMBER,favorableNumberStr);
                    }
                } else {
                    setClick(false);
                    etDiscountPay.setEnabled(true);
                }
                setResult(ContantsIntent.DISCOUNTRRESULTCODE,intent);
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        tvDiscount10.setSelected(true);
        etDiscountPay.setText("10.0");
        discountList.add(tvDiscount1);
        discountList.add(tvDiscount2);
        discountList.add(tvDiscount3);
        discountList.add(tvDiscount4);
        discountList.add(tvDiscount5);
        discountList.add(tvDiscount6);
        discountList.add(tvDiscount7);
        discountList.add(tvDiscount8);
        discountList.add(tvDiscount9);
        discountList.add(tvDiscount10);
        list.addAll(discountList);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_discount1:   //折扣1
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("1.0");
                break;
            case R.id.tv_discount2:   //折扣2
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("2.0");
                break;
            case R.id.tv_discount3:   //折扣3
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("3.0");
                break;
            case R.id.tv_discount4:   //折扣4
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("4.0");
                break;
            case R.id.tv_discount5:   //折扣5
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("5.0");
                break;
            case R.id.tv_discount6:   //折扣6
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("6.0");
                break;
            case R.id.tv_discount7:   //折扣7
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("7.0");
                break;
            case R.id.tv_discount8:   //折扣8
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("8.0");
                break;
            case R.id.tv_discount9:   //折扣9
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("9.0");
                break;
            case R.id.tv_discount10:   //折扣10
                selectedSet(String.valueOf(v.getId()), discountList);
                etDiscountPay.setText("10.0");
                break;

            case R.id.tv_favorable:
                llDiscount.setVisibility(View.GONE);
                tvDiscount.setBackgroundResource(R.drawable.corner_gray);
                llFavorable.setVisibility(View.VISIBLE);
                tvFavorable.setBackgroundResource(R.drawable.corner_blue);
                etDiscountPay.setText("");
                break;
            case R.id.tv_discount:     //折扣按钮
                llDiscount.setVisibility(View.VISIBLE);
                tvDiscount.setBackgroundResource(R.drawable.corner_blue);
                llFavorable.setVisibility(View.GONE);
                tvFavorable.setBackgroundResource(R.drawable.corner_gray);
                etFavorableNumber.setText("");
                etFavorableParticulars.setText("优惠信息:");
                break;

        }

    }

    //设置点击事件
    public void setClick(boolean b) {
        if (b) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setOnClickListener(null);  //关闭所有点击事件
            }
        } else {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setOnClickListener(this);
            }
        }
    }
    //设置选中状态
    public void selectedSet(String id, List<View> list1) {
        for (int i = 0; i < list1.size(); i++) {
            if (!id.equals(String.valueOf(list1.get(i).getId()))) {
                list1.get(i).setSelected(false);
            } else {
                list1.get(i).setSelected(true);
            }
        }
    }

}
